//
//  SaleDetViewController.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/20.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "SaleDetViewController.h"

#import "SurroundViewController.h"
#import "CustomSaleDetView.h"
#import "SaleModel.h"
#import "UIImageView+WebCache.h"
#import "ConnectModel.h"
#import "CustomSaleCell.h"
@interface SaleDetViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation SaleDetViewController
- (void)dealloc
{
    [_salTable release];
    [_salScroll release];
    [_tableArray release];
    [_heartButton release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailModel = [[[SaleModel alloc] init] autorelease];
        self.tableArray = [NSMutableArray array];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationItem setTitle:@"特价旅行详情"];
    
    [self createScroll];
    [self createView];
    [self connect];
    [self createTable];
    [self createRightButton];
}

#pragma mark - 创建右按钮
- (void)createRightButton
{
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    [button setTitle:@"团队旅游" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickRightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
    [button release];
}

- (void)createScroll
{
    self.salScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, (self.view.frame.size.height - 64))];
    
    [self.salScroll setScrollEnabled:YES];
//    [_salScroll setPagingEnabled:YES];
    [_salScroll setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
//    _salScroll.bounces = NO;
    [_salScroll setAlwaysBounceVertical:YES];
//    [_salScroll setShowsVerticalScrollIndicator:YES];
    [self.view addSubview:_salScroll];
    [_salScroll release];
}

-(void)buttonClick:(UIButton *)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//传值
- (void)getModel:(SaleModel *)model
{
    self.detailModel = model;

}
- (void)createView
{
    CustomSaleDetView *cView = [[CustomSaleDetView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3 * 2)];
    [cView.image setImageWithURL:[NSURL URLWithString:self.detailModel.cover]];
    cView.nameLabel.text = self.detailModel.title;
    cView.priceLabel.text =[ NSString stringWithFormat:@"Price:%@", self.detailModel.market_price ];
    cView.startAreaLabel.text = self.detailModel.depart_place;
    cView.commentLabel.text = @"评论";
    [cView.wantButton setTitle:@"想去:" forState:UIControlStateNormal];
    _heartButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _heartButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 + 135, cView.image.frame.size.height + 45, 20, 20);
    [_heartButton setTitle:@" " forState:UIControlStateHighlighted];
    [_heartButton setBackgroundImage:[UIImage imageNamed:@"whiteStar"] forState:UIControlStateNormal];
    [_heartButton addTarget:self action:@selector(heartButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cView addSubview:_heartButton];
//    [_heartButton release];
    [self.salScroll addSubview:cView];
    [cView release];
}
#pragma mark - heartButton事件
- (void)heartButtonAction:(id)sender
{
    if ([[self.heartButton titleForState:UIControlStateHighlighted]isEqualToString:@" "])
    {
        [self.heartButton setBackgroundImage:[UIImage imageNamed:@"redStar.png"] forState:UIControlStateNormal];
        
        [self.heartButton setTitle:@"  " forState:UIControlStateHighlighted];
    }
    else
    {
   
        [self.heartButton setBackgroundImage:[UIImage imageNamed:@"whiteStar.png"] forState:UIControlStateNormal];
        [self.heartButton setTitle:@" " forState:UIControlStateHighlighted];
    }

}

#pragma mark - 创建tableview
- (void)createTable
{
    self.salTable = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 3 * 2, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    self.salTable.tableFooterView = [[[UIView alloc] init] autorelease];
    [_salTable setDelegate:self];
    [_salTable setDataSource:self];
    
    [_salTable setRowHeight:80];
    
    [self.salScroll addSubview:_salTable];
    [_salTable release];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_tableArray count] < 2) {
        [_salScroll setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 100)];
    }
    else
    {
        [_salScroll setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height + (([_tableArray count] - 1 ) * 50))];
    }
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"salPool";
    CustomSaleCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell = [[[CustomSaleCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID] autorelease];
    }
   
    NSMutableDictionary *d = [_tableArray objectAtIndex:indexPath.row];
    cell.userNameLabel.text = [NSString stringWithFormat:@"%@:🎺",[d objectForKey:@"username"]];
    
    cell.commentLabel.text = [d objectForKey:@"comment"];
    
    return cell;
}

#pragma mark - 点击事件
- (void)clickRightButtonAction:(id)sender
{
    SurroundViewController *surVC = [[SurroundViewController alloc] init];
    
    [self.navigationController pushViewController:surVC animated:YES];
    [surVC release];
}

#pragma mark - ID
- (void)getID:(NSInteger)ID
{
    self.getID = ID;

}

#pragma  mark - JOSN
- (void)connect
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://breadtrip.com/btcomment/?uri=tp-%ld&count=5", self.getID]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *arr = [dic objectForKey:@"comments"];
        for (NSMutableDictionary *sdic in arr) {
            [self.tableArray addObject:sdic];
        }
       
        [_salTable reloadData];
    }];
    
}




@end
